<template>
  <div class="hyydxq">
    <!-- 选择销售批次 -->
    <van-search clearable background="transparent" v-model="keyword" class="b-b-2" placeholder="请输入关键词进行搜索" @search="onSearch" />
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <van-checkbox-group v-model="result">
          <van-cell-group style='background: #00000000'>
            <van-cell v-for="(item, index) in list" clickable :key="index" class="approvalItem content"  @click="toggles(index)">
              <div class="flex-between font-13 line-height-3">
                <div class="col-666666 m-r-10 line-text-out" style='width: 90%'>
                  <p>{{ item.sourceEnterpriseName }}</p>
                  <p>销售批次：{{ item.salesBatchNo }}</p>
                  <p>生鲜乳总数量（kg）：{{ item.freshMilkTotal }}</p>
                </div>
              </div>
              <template #right-icon>
                <van-checkbox :name="item.id" ref="checkboxes" />
              </template>
            </van-cell>
          </van-cell-group>
        </van-checkbox-group>
      </van-list>
    </van-pull-refresh>
    <div class="add-btn">
      <van-button
        block
        type="info"
        color="#37B5B5"
        style="width: 85%; margin: auto; margin-bottom: 50px"
        @click='onSubmit'
        size="large"
        :loading="loading">确定</van-button>
    </div>
  </div>
</template>

<script>
import { getValue, setValue } from '@/utils/auth'
import { Toast, Cell, CellGroup, Popup, Picker, DatetimePicker, PullRefresh, List, Search, Button, CheckboxGroup, Checkbox } from 'vant'
import { getHandle } from '@/api'
export default {
  components: {
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
    [Popup.name]: Popup,
    [Picker.name]: Picker,
    [DatetimePicker.name]: DatetimePicker,
    [PullRefresh.name]: PullRefresh,
    [Toast.name]: Toast,
    [List.name]: List,
    [Search.name]: Search,
    [Button.name]: Button,
    [CheckboxGroup.name]: CheckboxGroup,
    [Checkbox.name]: Checkbox
  },
  data() {
    return {
      radio: 0,
      keyword: '',
      refreshing: false,
      currentDate: new Date(),
      list: [],
      result: [],
      loading: false,
      finished: false,
      params: {
        page: 1,
        limit: 10,
        enterpriseName: '',
        tenantCode: getValue('tenantCode'),
        // subjectInfoId: getValue('subjectInfoId'),
        salesStatus: 1,
        type: 1,
        whereaboutsEnterpriseId: getValue('subjectInfoId')
      }
    }
  },
  watch: {},
  created() {
    const selectDales = getValue('selectDales') ? JSON.parse(getValue('selectDales')) : []
    if (selectDales) {
      this.result = selectDales.map(item=>item.id)
    }
  },
  mounted() {},
  methods: {
    // 卡片列表
    onRefresh() {
      this.params.page = 1
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    onSearch(val) {
      this.params.page = 1
      this.list = []
      this.finished = false
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.params.enterpriseName = val
      this.onLoad()
    },
    onLoad() {
      getHandle('/agri/api/yzbzh/saleInfo/queryListPage', this.params).then(res => {
        if (this.refreshing) {
          this.list = []
          this.refreshing = false
        }
        this.list = this.list.concat(res.data.rows)
        this.loading = false

        if (this.list.length >= res.data.count) {
          this.finished = true
        } else {
          this.params.page = this.params.page + 1
        }
      })
    },
    toggles(index) {
      this.$refs.checkboxes[index].toggle()
    },
    onSubmit() {
      let data = this.list.filter((item => this.result.includes(item.id)))
      setValue('selectDales', JSON.stringify(data))
      this.$bridge.call('base.closePage')
    }
  }
}
</script>

<style lang="scss" scoped>
.topCeiling {
  position: fixed;
  top: 0;
  z-index: 1;
}
.add-btn {
  padding-bottom: 20px;
}
.van-field__control {
  border: 0 !important;
}
.content {
  //   border-bottom: 1px solid #eee;
  padding: 10px 15px;
}
::v-deep .van-search{
    width: 98%;
    margin: auto;
}
::v-deep .van-search__content{
    background-color: #fff;
    border-radius: 34px;

}
</style>
